var Config = require("../config");
var KOC = require("koc.common.utils");
var crypto = require("crypto");
module.exports = {
    RefreshUrl: function (url, callback) {
        var parm = {
            Signature: null,
            Timestamp: parseInt(Date.now() / 1000),
            Action: 'RefreshCdnUrl',
            Nonce: KOC.Lodash.random(10000, 99999),
            options: {},
            body: ''
        };
        if (typeof url === "string") {
            url = [url];
        }
        //判断数组
        if (!KOC.Lodash.isArray(url)) {
            callback(KOC.ReturnValue.New({
                hasError: true,
                message: 'url地址不是数组'
            }));
            return;
        }
        //最大数量
        if (url.length > 10) {
            callback(KOC.ReturnValue.New({
                hasError: true,
                message: "更新的地址不能超过10个"
            }));
            return;
        }
        parm.Signature = 'POSTcdn.api.qcloud.com/v2/index.php?' +
            'Action=' + parm.Action +
            '&Nonce=' + parm.Nonce +//随机整数
            '&SecretId=' + Config.Interface.QCloud.SecretId +
            '&Timestamp=' + parm.Timestamp;//当前时间戳
        for (var i = 0; i < url.length; i++) {
            if (url[i].indexOf('_') != -1) {
                url[i] = url[i].split('_').join('.');
            }
            parm.Signature += '&urls.' + i + '=' + url[i];
        }
        parm.body += 'Signature=' + encodeURIComponent(crypto.createHmac('sha1', Config.Interface.QCloud.SecretKey).update(parm.Signature).digest().toString('base64'));
        parm.body += '&Action=' + parm.Action +
            '&Nonce=' + parm.Nonce +//随机整数
            '&SecretId=' + Config.Interface.QCloud.SecretId +
            '&Timestamp=' + parm.Timestamp;//当前时间戳
        for (var i = 0; i < url.length; i++) {
            parm.body += '&urls.' + i + '=' + encodeURIComponent(url[i]);
        }
        parm.options = {
            url: "https://cdn.api.qcloud.com/v2/index.php",
            method: "post",
            form: parm.body
        };
        KOC.Request.post(parm.options, function (err, httpResponse, body) {
            if (err) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: err.message
                }));
                return;
            }
            try {
                body = JSON.parse(body);
            } catch (ex) {
                body = null;
            }
            if (!body) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: '返回信息出错！'
                }));
                return;
            }
            if (body.code != 0) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: body.message + body.code
                }));
                return;
            }
            callback(KOC.ReturnValue.New());
        });
    },
    RefreshDir: function (url, callback) {
        var parm = {
            Signature: null,
            Timestamp: parseInt(Date.now() / 1000),
            Action: 'RefreshCdnDir',
            Nonce: KOC.Lodash.random(10000, 99999),
            options: {},
            body: ''
        };
        if (typeof url === "string") {
            url = [url];
        }
        //判断数组
        if (!KOC.Lodash.isArray(url)) {
            callback(KOC.ReturnValue.New({
                hasError: true,
                message: 'url地址不是数组'
            }));
            return;
        }
        //最大数量
        if (url.length > 10) {
            callback(KOC.ReturnValue.New({
                hasError: true,
                message: "更新的地址不能超过10个"
            }));
            return;
        }
        parm.Signature = 'POSTcdn.api.qcloud.com/v2/index.php?' +
            'Action=' + parm.Action +
            '&Nonce=' + parm.Nonce +//随机整数
            '&SecretId=' + Config.Interface.QCloud.SecretId +
            '&Timestamp=' + parm.Timestamp;//当前时间戳
        for (var i = 0; i < url.length; i++) {
            if (url[i].indexOf('_') != -1) {
                url[i] = url[i].split('_').join('.');
            }
            parm.Signature += '&dirs.' + i + '=' + url[i];
        }
        parm.body += 'Signature=' + encodeURIComponent(crypto.createHmac('sha1', Config.Interface.QCloud.SecretKey).update(parm.Signature).digest().toString('base64'));
        parm.body += '&Action=' + parm.Action +
            '&Nonce=' + parm.Nonce +//随机整数
            '&SecretId=' + Config.Interface.QCloud.SecretId +
            '&Timestamp=' + parm.Timestamp;//当前时间戳
        for (var i = 0; i < url.length; i++) {
            parm.body += '&dirs.' + i + '=' + encodeURIComponent(url[i]);
        }
        parm.options = {
            url: "https://cdn.api.qcloud.com/v2/index.php",
            method: "post",
            form: parm.body
        };
        KOC.Request.post(parm.options, function (err, httpResponse, body) {
            if (err) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: err.message
                }));
                return;
            }
            try {
                body = JSON.parse(body);
            } catch (ex) {
                body = null;
            }
            if (!body) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: '返回信息出错！'
                }));
                return;
            }
            if (body.code != 0) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: body.message + body.code
                }));
                return;
            }
            callback(KOC.ReturnValue.New());
        });
    }
};